<?php
include_once("../config.php");
$sql="select id,name,shang from fenlei where 1=1";
$requ = mysqli_query($con,$sql);
$items = array();//故障类型
$gzxm = array();//故障项目
while($rs=mysqli_fetch_array($requ)){
	$a=array("title"=>$rs['name'],"value"=>$rs['id'],"pid"=>$rs['shang']);
	array_push($items,$a);
	if($rs['shang']==0){
		array_push($gzxm,$a);
	}
}
mysqli_free_result($requ);

$sql = "select id,name from danwei where status=1 and other='0'";
$requ = mysqli_query($con,$sql);
$dw=array();//单位
while($rs=mysqli_fetch_array($requ)){
	$a=array("title"=>$rs['name'],"value"=>$rs['id'],"pid"=>0);
	array_push($dw,$a);
}
mysqli_free_result($requ);


$uid = $_GET['uid'];
$uid = str_ireplace($search, '', $uid);

$sql = "select user.ming as ming,user.tel as tel,danwei.name as dw,danwei.id as id from user 
		left join danwei on user.danwei=danwei.id where user.openid='$uid'";
$requ = mysqli_query($con,$sql);
$rs=mysqli_fetch_array($requ);
$ming = $rs['ming'];
$tel = $rs['tel'];
$dwname = $rs['dw'];
$dwid = $rs['id'];

$generateTree = function ($items, $parentKey = 'pid') {
    $tree = $temp = array();
    foreach ($items as $item) {
        $temp[$item['value']] = $item;
    }
    foreach ($items as $item) {
        if (isset($temp[$item[$parentKey]])) {
            $temp[$item[$parentKey]]['list'][] = &$temp[$item['value']];
        } else {
            $tree[] = &$temp[$item['value']];
        }
        unset($temp[$item['value']][$parentKey]);
    }
    return $tree;
};

$sql = "select id from wenti where status=1 order by id desc limit 20";
$requ = mysqli_query($con,$sql);
$wtnum = mysqli_num_rows($requ);

$result='{"status":"1","msg":"ok","dwid":"'.$dwid.'",
		  "dcl":"'.$wtnum.'",
		  "ming":"'.$ming.'","tel":"'.$tel.'","dw":"'.$dwname.'",
		  "gzxm":'.json_encode($generateTree($gzxm)).',
		  "danwei":'.json_encode($generateTree($dw)).',
		  "gzlx":'.json_encode($generateTree($items)).'}';
echo $result;









